<template>
    <div>
        <el-row class="lock-btn">
            <el-button-group>
                <el-button :disabled="userState" size="medium" type="primary" @click="addBtn" round>添加</el-button>
            </el-button-group>
        </el-row>
        <el-row style="box-shadow: rgb(205 205 205) 1px 1px 8px;">
            <el-col :span="24">
                <el-table :header-cell-style="{ background: '#ecf3fd',color:'#000000' }" highlight-current-row  ref="blackListTable" v-loading="loading" :data="blackLists" :stripe=true :row-key="'id'" :max-height="clientHeight" :height="clientHeight" @selection-change="handleTableChange">
                    <el-table-column prop="openId" label="openId"  width="auto" align="center">
                    </el-table-column>
                    <el-table-column prop="reason" label="原因" width="auto" align="center">
                    </el-table-column>
                    <el-table-column prop="exceptionNum" label="异常次数" sortable width="auto" align="center">
                    </el-table-column>
                    <el-table-column prop="createTime" label="时间" width="auto" align="center">
                    </el-table-column>
                    <el-table-column label="操作" width="120"  align="center">
                        <template slot-scope="scope">
                            <el-button :disabled="userState" class="operateBtn" size="mini" type="primary" @click="editBtn(scope.$index, scope.row)" title="编辑"><i class="el-icon-edit-outline"></i></el-button>
                            <el-button :disabled="userState" class="operateBtn" size="mini" type="danger" @click="delBtn(scope.$index, scope.row)" title="删除"><i class="el-icon-delete"></i></el-button>
                        </template>
                    </el-table-column>
                </el-table>
            </el-col>
        </el-row>
        <el-row>
            <el-col :span="24" class="page-tool">
                <el-pagination layout="total,sizes,prev, pager,next,jumper" :page-size="pageSize" :total="total" :page-sizes="[10, 20, 30, 40, 100]" @size-change="handlePageChange" @current-change="handleNoChange">
                </el-pagination>
            </el-col>
        </el-row>
        <el-dialog :title="dialogTitle" :visible.sync="addDialog" :width="'422px'">
            <el-form :model="form" :rules="rules" ref="form">
                <el-form-item label="openId" :label-width="labelWidth" prop="openId">
                    <el-input v-model="form.openId " auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="原因" :label-width="labelWidth" prop="reason">
                    <el-input v-model="form.reason " auto-complete="off"></el-input>
                </el-form-item>
                <el-form-item label="异常次数 " :label-width="labelWidth" prop="exceptionNum">
                    <el-input v-model="form.exceptionNum " auto-complete="off"></el-input>
                </el-form-item>
            </el-form>
            <div slot="footer" class="dialog-footer">
                <el-button @click="addDialog = false">取 消</el-button>
                <el-button type="primary" @click="saveBlackList">确 定</el-button>
            </div>
        </el-dialog>
    </div>
</template>
<script>  
import {
  loadBlackListApi,
  delBlackListApi,
  addBlackListApi,
  updateBlackListApi
} from "../../api/blackList.js";
import { getNickName, getTestUserName } from "../../util/auth";
export default {
  name: "runInfo",
  data: function() {
    return {
      clientHeight:
        Math.max(
          document.documentElement.scrollHeight,
          document.body.scrollHeight
        ) -
        132 -
        5 +
        10 -
        28,
      labelWidth: "100px",
      loading: false,
      dialogTitle: "",
      pageNo: 1,
      pageSize: 20,
      total: 0,
      multipleSelection: [],
      blackLists: [],
      form: {},
      addDialog: false,

      rules: {
        openId: [
            { required: true, message: '请输入openId', trigger: 'blur' }
          ],
        reason: [
            { required: true, message: '请输入原因', trigger: 'blur' }
          ],
        exceptionNum: [
            { required: true, message: '异常次数', trigger: 'blur' }
          ],
      },
    };
  },
  methods: {
    saveBlackList() {
      let _this = this;
       _this.$refs.form.validate((valid) => {
        if (valid) {
          if (_this.dialogTitle === "添加黑名单") {
            addBlackListApi(_this.form).then(resp => {
              _this.addDialog = false;
              if (resp.errmsg === "ok") {
                _this.loadBlackLists(_this.pageNo, _this.pageSize);
                _this.$notify({
                  title: "成功",
                  message: "黑名单添加成功",
                  type: "success"
                });
              } else {
                _this.$notify({
                  title: "失败",
                  message: "黑名单添加失败",
                  type: "error"
                });
              }
            });
          } else {
            updateBlackListApi(_this.form).then(resp => {
              _this.addDialog = false;
              if (resp.errmsg === "ok") {
                _this.loadBlackLists(_this.pageNo, _this.pageSize);
                _this.$notify({
                  title: "成功",
                  message: "黑名单修改成功",
                  type: "success"
                });
              } else {
                _this.$notify({
                  title: "失败",
                  message: "黑名单修改失败",
                  type: "error"
                });
              }
            });
          }
        }
      });
    },
    addBtn() {
      let _this = this;
      _this.$nextTick(()=>{
        if(_this.$refs.form != undefined){
          _this.$refs.form.resetFields();
        }
      })
      _this.dialogTitle = "添加黑名单";
      _this.form = {};
      _this.addDialog = true;
    },
    editBtn(index, row) {
      let _this = this;
      _this.$nextTick(()=>{
        if(_this.$refs.form != undefined){
          _this.$refs.form.resetFields();
        }
      })
      _this.dialogTitle = "修改黑名单";
      _this.form = row;
      _this.addDialog = true;
    },
    delBtn(index, row) {
      let _this = this;
      this.$confirm('确认删除黑名单“' + row.openId + '”', '提示', {
        type: 'warning'
      })
      .then(_ =>{
        delBlackListApi(row.id).then(resp => {
          if (resp.errmsg === "ok") {
            _this.loadBlackLists(this.pageNo, this.pageSize);
            _this.$notify({
              title: "成功",
              message: "黑名单删除成功",
              type: "success"
            });
          }
        });
      })
    },
    roleIdFormatter(row, column) {
      if (row.roleId === 1) {
        return "管理员";
      }else if (row.roleId === 2){
        return "代理商";
      }else if (row.roleId === 3){
        return "运营商";
      }else {
        return "";
      }
    },
    handlePageChange(pageSize) {
      this.pageSize = pageSize;
      this.loadBlackLists(this.pageNo, this.pageSize);
    },
    handleNoChange(pageNo) {
      this.pageNo = pageNo;
      this.loadBlackLists(this.pageNo, this.pageSize);
    },
    handleTableChange(val) {
      this.multipleSelection = val;
    },
    loadBlackLists(pageNo, pageSize) {
      let _this = this;
      _this.loading = true;
      loadBlackListApi(pageNo, pageSize).then(resp => {
        console.log(resp)
        _this.blackLists = resp.data.dataList;
        _this.total = resp.data.totalNum;
        _this.loading = false;
      });
    }
  },
  computed: {
    userState () {
      return getNickName() === getTestUserName();
    }
  },
  mounted: function() {
    this.$nextTick(function() {
      this.loadBlackLists(this.pageNo, this.pageSize);
    });
  }
};
</script>

<style scoped>


.page-tool div {
  text-align: right;
}

.lock-btn {
  margin-bottom: 5px;
  margin-top: -10px;
}
.operateBtn {
  font-size: 18px;
  width: 32px;
  height: 32px;
  padding: 0;
}
</style>